package com.ujson.cloud.alibaba.sentinel.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.ujson.base.common.dto.Res;
import com.ujson.base.common.utils.LogUtil;
import com.ujson.base.common.utils.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <h3>ujson-parent</h3>
 * <p></p>
 *
 * @author JerryLiu  a1031757312@gmail.com
 * @version 1.0
 * @date 2021-11-21 17:13
 */
@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {

    @RequestMapping(value = "demo1", method = {RequestMethod.GET})
    public Object demo1() {
        Entry entry = null;
        try {
            entry = SphU.entry("demo1");
            ThreadUtil.sleep(100L);
        } catch (BlockException ex) {
            LogUtil.error(log, "sentinel block.", ex);
            return Res.fail();
        } finally {
            if (Objects.nonNull(entry)) {
                entry.exit();
            }
            ContextUtil.exit();
        }
        return Res.success("demo1");
    }

}
